//------------------------------------------------------------------------------
//  <copyright file="Line2DTests.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//  </copyright>
//------------------------------------------------------------------------------

namespace Test.Robotics.Geometry
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    using Microsoft.Robotics.Geometry;
    using Microsoft.Robotics.Numerics;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Tests for Plane2DClass
    /// </summary>
    [TestClass]
    public class Line2DTests
    {
        /// <summary>
        /// Test the constructor for Line2D
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void Line2DPointVectorConstructorTest()
        {
            Vector2 n = new Vector2(1, -1);
            Line2D pointVectorLine = new Line2D(new Point2D(1, 3), n);

            n = Vector2.Normalize(n);
            Assert.AreEqual(n, pointVectorLine.N);
            Assert.AreEqual(new Point2D(1, 3), pointVectorLine.A);
        }

        /// <summary>
        /// Test the constructor for Line2D
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void Line2DPointPointConstructorTest()
        {
            Vector2 n = new Vector2(1, -1);
            Line2D pointVectorLine = new Line2D(new Point2D(1, 3), new Point2D(2, 2));

            n = Vector2.Normalize(n);
            Assert.AreEqual(n, pointVectorLine.N);
            Assert.AreEqual(new Point2D(1, 3), pointVectorLine.A);
        }

        /// <summary>
        /// Test the constructors for Line2D
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void Line2DConstructorsProduceEquivalentLinesTest()
        {
            // confirm that resulting object is the same whether we use point/vector, 
            // slope/intercept, or point/point method of creating line
            Line2D pointVectorLine = new Line2D(new Point2D(0, 3), new Vector2(1, -1));
            Line2D pointPointLine = new Line2D(new Point2D(0, 3), new Point2D(3, 0));

            Assert.AreEqual(pointVectorLine.N, pointPointLine.N);
            Assert.AreEqual(pointVectorLine.A, pointPointLine.A);

            pointVectorLine = new Line2D(new Point2D(0, -5), new Vector2(1, 3));
            pointPointLine = new Line2D(new Point2D(0, -5), new Point2D(5.0 / 3.0, 0));

            Assert.AreEqual(pointVectorLine.N, pointPointLine.N);
            Assert.AreEqual(pointVectorLine.A, pointPointLine.A);
        }

        /// <summary>
        /// Test the Line2D distance form point method
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void Line2DDistanceFromPointTest()
        {
            Line2D line = new Line2D(new Point2D(0, 0), new Vector2(0, 1));
            Point2D point = new Point2D(3, 10);
            Assert.AreEqual(3.0, line.DistanceFrom(point), MathConstants.ErrorEpsilon);

            line = new Line2D(new Point2D(3, 0), new Vector2(-1, 1));
            point = new Point2D(-1, -1);
            Assert.AreEqual(5.0 * Math.Sin(MathConstants.PIOverFour), line.DistanceFrom(point), MathConstants.ErrorEpsilon);
        }

        /// <summary>
        /// Test the intersection between two Line2D objects.
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void Line2DIntersectionWithLineTest()
        {
            Line2D line1 = new Line2D(new Point2D(0, 0), new Vector2(1, 0));
            Line2D line2 = new Line2D(new Point2D(0, 0), new Vector2(0, 1));

            Assert.AreEqual(new Point2D(0, 0), line1.GetIntersectionPoints(line2)[0]);
            Assert.AreEqual(new Point2D(0, 0), line2.GetIntersectionPoints(line1)[0]);

            line1 = new Line2D(new Point2D(-1, -1), new Vector2(1, 1));
            line2 = new Line2D(new Point2D(3, 0), new Vector2(-1, 1));

            Assert.AreEqual(new Point2D(1.5, 1.5), line1.GetIntersectionPoints(line2)[0]);
            Assert.AreEqual(new Point2D(1.5, 1.5), line2.GetIntersectionPoints(line1)[0]);
        }

        /// <summary>
        /// Test the intersection between two parallel Line2D objects.
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void Line2DIntersectionWithParallelLineTest()
        {
            Line2D line1 = new Line2D(new Point2D(0, 0), new Vector2(1, 0));
            Line2D line2 = new Line2D(new Point2D(3, 3), new Vector2(1, 0));

            List<Point2D> intersectionList = line1.GetIntersectionPoints(line2);
            Assert.AreEqual(0, intersectionList.Count);
        }

        /// <summary>
        /// Test intersection with a circle that intersects the line at two points.
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void Line2DIntersectionWithCircleAtTwoPointsTest()
        {
            Point2D center = new Point2D(3, 2);
            double radius = 2;
            Circle circle = new Circle(center, radius);

            Point2D firstIntersectionPoint = new Point2D(3, 4);
            Point2D secondIntersectionPoint = new Point2D(5, 2);
            Line2D line = new Line2D(firstIntersectionPoint, secondIntersectionPoint);

            List<Point2D> intersectionPoints = line.GetIntersectionPoints(circle);
            Assert.IsTrue(intersectionPoints.Contains(firstIntersectionPoint));
            Assert.IsTrue(intersectionPoints.Contains(secondIntersectionPoint));
        }

        /// <summary>
        /// Test intersection with a circle that intersects the line at one point.
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void Line2DIntersectionWithCircleAtOnePointTest()
        {
            Point2D center = new Point2D(3, 2);
            double radius = 2;
            Circle circle = new Circle(center, radius);

            Point2D intersectionPoint = new Point2D(3, 4);

            Line2D line = new Line2D(intersectionPoint, new Vector2(1, 0));

            List<Point2D> intersectionPoints = line.GetIntersectionPoints(circle);
            Assert.IsTrue(intersectionPoints.Contains(intersectionPoint));
            Assert.IsTrue(intersectionPoints.Count == 1);
        }

        /// <summary>
        /// Test intersection with a circle that do not intersect.
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void Line2DIntersectionWithCircleAtZeroPointsTest()
        {
            Point2D center = new Point2D(-1, -1);
            double radius = 2;
            Circle circle = new Circle(center, radius);

            Point2D intersectionPoint = new Point2D(3, 4);

            Line2D line = new Line2D(intersectionPoint, new Vector2(1, 0));

            List<Point2D> intersectionPoints = line.GetIntersectionPoints(circle);
            Assert.IsTrue(intersectionPoints.Count == 0);
        }

        /// <summary>
        /// Test that an exception is thrown if points passed to <c>Line2D</c>
        /// are not unique.
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        [ExpectedException(typeof(ArgumentException))]
        public void Line2DNonUniquePointsConstructorTest()
        {
            Line2D l = new Line2D(new Point2D(1, 1), new Point2D(1, 1));
        }

        /// <summary>
        /// Test that an exception is thrown if vector passed to <c>Line2D</c>
        /// has zero length.
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        [ExpectedException(typeof(ArgumentException))]
        public void Line2DZeroLengthConstructorTest()
        {
            Line2D l = new Line2D(new Point2D(1, 1), new Vector2(0, 0));
        }

        /// <summary>
        /// Test intersection test between Line2D and Line2D
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void Line2DLine2DIntersectionTest()
        {
            Line2D line = new Line2D(new Point2D(-3, -2), new Point2D(4, 3));
            Line2D intersectingLine = new Line2D(new Point2D(-3, 5), new Point2D(3, -2));
            Line2D nonIntersectingLine = new Line2D(new Point2D(-3, -3), new Point2D(4, 2));

            Assert.IsTrue(line.Intersects(intersectingLine));
            Assert.IsFalse(line.Intersects(nonIntersectingLine));
        }

        /// <summary>
        /// Test intersection test between Line2D and LineSegment2D
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void Line2DLineSegment2DIntersectionTest()
        {
            Line2D line = new Line2D(new Point2D(-3, -2), new Point2D(4, 3));
            LineSegment2D intersectingLineSegment = new LineSegment2D(new Point2D(-3, 5), new Point2D(3, -2));
            LineSegment2D nonIntersectingLineSegment = new LineSegment2D(new Point2D(-3, 5), new Point2D(-1, 2));
            LineSegment2D nonIntersectingLineSegment2 = new LineSegment2D(new Point2D(-3, -3), new Point2D(4, 2));

            Assert.IsTrue(line.Intersects(intersectingLineSegment));
            Assert.IsFalse(line.Intersects(nonIntersectingLineSegment));
            Assert.IsFalse(line.Intersects(nonIntersectingLineSegment2));
        }
    }
}
